package com.aki.ai_property.controller;

import com.aki.ai_property.dto.SellLesseeDto;
import com.aki.ai_property.dto.LesseeExportDto;
import com.aki.ai_property.dto.SellLesseeDto;
import com.aki.ai_property.dto.SubscriptionExportDto;
import com.aki.ai_property.entity.Lessee;
import com.aki.ai_property.service.intf.LesseeService;
import com.aki.ai_property.vo.PageBean;
import com.aki.ai_property.vo.Result;
import com.alibaba.excel.EasyExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

import java.util.List;

/**
 * ━━━━━━永无BUG!━━━━━━
 *
 * @author AKI
 * @date 2022/8/27
 */
@RestController
@RequestMapping("/api/lessee/")
public class LesseeController {
    @Autowired
    private LesseeService service;

    @PostMapping("save.do")
    public Result save(Lessee lessee){
        return service.save(lessee);
    }
    @GetMapping("delete.do")
    public Result delete(Integer id){
        return service.delete(id);
    }
    @PostMapping("update.do")
    public Result update(Lessee lessee){
        return service.update(lessee);
    }
    @GetMapping("page.do")
    public PageBean page(String name,int page,int limit){
        return service.selectPage(name,page,limit);
    }
//    @GetMapping("all1.do")
//    public Result all(){
//        return service.all();
//    }

    @GetMapping("alldto.do")
    public Result allDto(){
        return service.allDto();
    }
    /**导出*/
    @GetMapping("download.do")
    public void download(HttpServletResponse response) throws IOException {
        //1.设置 响应消息头 下载
        response.setHeader("content-disposition","attachment;filename=lessee-"+System.currentTimeMillis()+".xlsx");
        //2.查询学员数据
        List<LesseeExportDto> list=service.allLessee();
        //3.实现数据写出 Excel生成
        EasyExcel.write(response.getOutputStream(),LesseeExportDto.class).sheet(System.currentTimeMillis()+"").doWrite(list);
    }

    //导入
    @PostMapping("upload.do")
    public Result upload(MultipartFile file) throws IOException {
        return service.upload(file);
    }
}
